import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.LockSupport;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author songZiHao
 * @version 1.0.0
 * @ClassName TestOne.java
 * @Description TODO
 * @createTime 2021年01月23日 15:29:00
 */
public class TestOne {

	public static void main(String[] args) {
		/*TestOne testOne = new TestOne();
		testOne.execute4();*/
		System.out.println(new BigDecimal("5111").divide(new BigDecimal(1000),3,
				RoundingMode.HALF_UP).toPlainString());
	}


	AtomicInteger threadNo = new AtomicInteger(1);


	public void execute() {
		Object object = new Object();
		List<String> list1 = Arrays.asList("A", "B", "C", "D", "E", "F", "G");
		List<Integer> list2 = Arrays.asList(1, 2, 3, 4, 5, 6);

		AtomicInteger threadNo = new AtomicInteger(1);
		String endString = "G";
		Integer endInteger = 6;
		new Thread(new Runnable() {
			@Override
			public void run() {
				synchronized (object) {
					for (String str : list1) {
						System.out.println(str);
						threadNo.set(2);
						try {
							object.notify();
							if (!str.equals(endString)) {
								object.wait();
							}
						} catch (Exception e) {
							e.printStackTrace();
						}
					}
				}
			}
		}).start();

		new Thread(new Runnable() {
			@Override
			public void run() {
				synchronized (object) {
					try {
						if (threadNo.get() != 2) {
							object.notify();
							object.wait();
						}
						for (Integer integer : list2) {
							System.out.println(integer);

							object.notify();
							if (!integer.equals(endString)) {
								object.wait();
							}

						}
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}
		}).start();

	}

	public void execute2() {
		ReentrantLock lock = new ReentrantLock();
		Condition condition = lock.newCondition();
		List<String> list1 = Arrays.asList("A", "B", "C", "D", "E", "F", "G");
		List<Integer> list2 = Arrays.asList(1, 2, 3, 4, 5, 6);
		Thread thread1, thread2;

		new Thread(new Runnable() {
			@Override
			public void run() {
				lock.lock();
				try {
					for (String string : list1) {
						System.out.println(string);
						condition.signal();
						condition.await();
					}
				} catch (Exception e) {
					e.printStackTrace();
				} finally {
					lock.unlock();
				}
			}

		}).start();

		new Thread(new Runnable() {
			@Override
			public void run() {
				lock.lock();
				try {
					for (Integer integer : list2) {
						System.out.println(integer);
						condition.signal();
						condition.await();
					}
				} catch (Exception e) {
					e.printStackTrace();
				} finally {
					lock.unlock();
				}
			}
		}).start();


	}


	public void execute3() {
		List<String> list1 = Arrays.asList("A", "B", "C", "D", "E", "F", "G");
		List<Integer> list2 = Arrays.asList(1, 2, 3, 4, 5, 6);
		Thread thread1, thread2;

		new Thread(new Runnable() {
			@Override
			public void run() {
				for (String string : list1) {
					while (threadNo.get() == 1) {
						System.out.println(string);
						threadNo.set(2);
					}
				}
			}

		}).start();
		new Thread(new Runnable() {
			@Override
			public void run() {
				for (Integer integer : list2) {
					while (threadNo.get() == 2) {
						System.out.println(integer);
						threadNo.set(1);
					}
				}
			}
		}).start();
	}

	public void execute4() {
		Object object = new Object();
		List<String> list1 = Arrays.asList("A", "B", "C", "D", "E", "F", "G");
		List<Integer> list2 = Arrays.asList(1, 2, 3, 4, 5, 6);

		AtomicInteger threadNo = new AtomicInteger(1);
		String endString = "G";
		Integer endInteger = 6;
		new Thread(new Runnable() {
			@Override
			public void run() {
				synchronized (object) {
					for (String str : list1) {
						System.out.println(str);
						System.out.println(Thread.currentThread().getName());
						threadNo.set(2);
						if (str.equals(endString)) {
							Thread.currentThread().destroy();
						}
						try {
							object.notify();
							object.wait();

						} catch (Exception e) {
							e.printStackTrace();
						}
					}
				}
			}
		},"threadA").start();

		new Thread(new Runnable() {
			@Override
			public void run() {
				synchronized (object) {
					try {
						if (threadNo.get() != 2) {
							object.notify();
							object.wait();
						}
						for (Integer integer : list2) {
							System.out.println(integer);
							System.out.println(Thread.currentThread().getName());
							if (integer.equals(endString)) {
								Thread.currentThread().destroy();
							}
							object.notify();

							object.wait();


						}
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}
		},"threadB").start();

	}
}
